<?php
/**
 * Created by PhpStorm.
 * User: Office
 * Date: 2017/12/29
 * Time: 11:22
 */

//有没有接收到数据
if(!empty($_POST)) {
    reg_data();
}

function reg_data(){
    header("content-type:text/html;charset:uft-8");
    $conn = mysqli_connect("localhost","root","root","login0");
    if(!$conn){
        exit("数据库连接失败");
    }
    $username = $_POST['user'];
    $pwd = md5($_POST['pwd']);
    $sql = "select * from user WHERE  username ='$username'";
    $re = mysqli_fetch_array(mysqli_query($conn,$sql));
    if($re){
        echo "<div style='color: red;margin-top: 20px;'>此用户名已创建</div>";
    }else{
        $sql = "insert into user (username,pwd)VALUE ('{$username}','{$pwd}')";
        mysqli_query($conn,$sql);
        if(!mysqli_affected_rows($conn)){
            exit("添加数据失败");
        }else{
            header("refresh:3;url=../index.php");
            print('<div style="margin-top: 20px;background-color: rgba(169,169,169,.7);">添加数据成功! 三秒后自动跳转登录页面。</div>');
        }
    }
}
?>



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户注册</title>
    <link rel="stylesheet" href="../css/blog.css">
</head>
<body>

<div class="box">
    <header>
        <h2>我的BLOG</h2>
    </header>
    <div class="content clearBox">
        <div class="left">
            <h5 class="title">用户注册</h5>
            <form action="register.php" method="post" name="regForm">
                <dl>
                    <dt>用户名：</dt>
                    <dd><input type="text" name="user"/></dd>
                </dl>

                <dl>
                    <dt>密码：</dt>
                    <dd><input type="password" name="pwd"/></dd>
                </dl>
                <dl>
                    <dt>确认密码：</dt>
                    <dd><input type="password" name="pwd2"/></dd>
                </dl>
                <div>
                    <button type="submit" name="btn">注册</button>
                    <a href="../index.php" name="login">登录</a>
                </div>
            </form>
        </div>
        <div class="right">
            <h5 class="title">关于我</h5>
            <p class="right-ctn">php初学者</p>
        </div>
    </div>
</div>

<div class="footer">CopyRight&nbsp;2017</div>

<script src="../js/login.js"></script>
<script>
    addLoadEvent(checkLogin("regForm"));
</script>
</body>
</html>


